<?php

/**
 *   OPENSHOP 管理中心期刊管理
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: catalog.php,v 1.0 2009/06/17 08:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');

$exc = new exchange($os->table("catalog"), $db, 'catalog_id', 'catalog_name');

/*------------------------------------------------------ */
//-- 期刊列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	//检查权限
	admin_priv();
	
    $smarty->assign('ur_here',      '目录期刊');
    $smarty->assign('action_link',  array('text' => '添加期刊', 'href' => 'catalog.php?act=add'));
    $smarty->assign('full_page',    1);
    
    $catalog_list = get_cataloglist();

    $smarty->assign('catalog_list', $catalog_list['catalog']);
    $smarty->assign('filter',       $catalog_list['filter']);
    $smarty->assign('record_count', $catalog_list['record_count']);
    $smarty->assign('page_count',   $catalog_list['page_count']);

    assign_query_info();
    
    $smarty->display('catalog_list.tpl');
    
}elseif($_REQUEST['act']=='add' || $_REQUEST['act']=='edit'){ 
	  	
    $is_add = $_REQUEST['act'] == 'add';
    
    $priv_str = $is_add ? 'add_catalog' : 'edit_catalog';
    admin_priv($priv_str);
    
    $smarty->assign('form_act', $is_add ? 'insert' : 'update');
    
    /* 初始化、取得优惠活动信息 */
    if ($is_add)
    {
        $catalog = array(
            'catalog_id'        => 0,
            'catalog_sn'        => '',
            'catalog_name'      => '',
            'catalog_alias'     => '',
            'start_time'        => local_date('Y-m-d H:i'),
            'end_time'          => local_date('Y-m-d H:i', strtotime('+1 week')),
            'catalog_desc'      => '',
            'catalog_status'     => 1
        );
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }
        $catalog_id = intval($_GET['id']);
        $catalog = catalog_info($catalog_id);
        if (empty($catalog))
        {
            sys_msg('您要操作的目录期刊不存在');
        }
    }
    $smarty->assign('catalog', $catalog);
    
    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', '添加目录期刊');
    }
    else
    {
        $smarty->assign('ur_here', '编辑目录期刊');
    }
    $href = 'catalog.php?act=list';
    if (!$is_add)
    {
        $href .= '&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => '目录期刊管理'));
        
	assign_query_info();
	
	$smarty->display('catalog_info.tpl');	
	
}elseif ($_REQUEST['act'] == 'insert' || $_REQUEST['act'] == 'update'){

    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert';
 
    $priv_str = $is_add ? 'add_catalog' : 'edit_catalog';
    
    /* 检查权限 */
 	admin_priv($priv_str);

    /* 检查名称是否重复 */
    $catalog_name = sub_str($_POST['catalog_name'], 255, false);
    if (!$exc->is_only('catalog_name', $catalog_name, intval($_POST['id'])))
    {
        sys_msg('该期刊名称已存在，请您换一个');
    }

    if (!$exc->is_only('catalog_sn', $_POST['catalog_sn'], intval($_POST['id'])))
    {
        sys_msg('该期刊号已存在，请您换一个');
    }
    
    /* 提交值 */
    $catalog = array(
        'catalog_id'        => intval($_POST['id']),
        'catalog_sn'        => $_POST['catalog_sn'],
        'catalog_name'      => $catalog_name,    
        'catalog_alias'     => sub_str($_POST['catalog_alias'], 255, false),
        'start_time'    => local_strtotime($_POST['start_time']),
        'end_time'      => local_strtotime($_POST['end_time']),        
        'catalog_desc'     => $_POST['catalog_desc'],
        'catalog_status'      => intval($_POST['catalog_status'])
    );
    
    /* 保存数据 */
    if ($is_add)
    {
        $db->autoExecute($os->table('catalog'), $catalog, 'INSERT');
        $catalog['catalog_id'] = $db->insert_id();
    }
    else
    {
        $db->autoExecute($os->table('catalog'), $catalog, 'UPDATE', "catalog_id = '$catalog[catalog_id]'");
    }

    /* 记日志 */
    if ($is_add)
    {
        admin_log($catalog['catalog_name'], 'add', 'catalog');
    }
    else
    {
        admin_log($catalog['catalog_name'], 'edit', 'catalog');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'catalog.php?act=add', 'text' => '继续添加目录期刊'),
            array('href' => 'catalog.php?act=list', 'text' => '返回目录期刊列表')
        );
        sys_msg('添加目录期刊成功', 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'catalog.php?act=list&' . list_link_postfix(), 'text' => '返回目录期刊列表')
        );
        sys_msg('编辑目录期刊成功', 0, $links);
    }
    
}elseif ($_REQUEST['act'] == 'remove'){
	
    check_authz_json('drop_catalog');

    $id = intval($_GET['id']);

    $sql = "SELECT catalog_name FROM " .$os->table('catalog'). " WHERE catalog_id = '$id'";
    $catalog_name = $db->getOne($sql);
    
    $catalog_page_no_count = $db->getOne('SELECT COUNT(*) FROM ' .$os->table('catalog_page_no'). " WHERE catalog_id='$id'");
    $catalog_p_no_goods_count = $db->getOne('SELECT COUNT(*) FROM ' .$os->table('catalog_p_no_goods'). " WHERE catalog_id='$id'");
    $catalog_send_count = $db->getOne('SELECT COUNT(*) FROM ' .$os->table('catalog_send'). " WHERE catalog_id='$id'");
    
    if($catalog_page_no_count==0 && $catalog_p_no_goods_count==0 && $catalog_send_count==0){
	    /* 删除该颜色 */
	    if($exc->drop($id)){
	    	clear_cache_files();
			admin_log($catalog_name, 'remove', 'catalog');	    	
	    }
    }else{
    	make_json_error('"'.$catalog_name .'" 该期刊与页码、页码商品和发行还有关联,您不能删除!');
    }
    
    $url = 'catalog.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

    os_header("Location: $url\n");
    exit;
    
}elseif ($_REQUEST['act'] == 'query'){
	
    $list = get_cataloglist();

    $smarty->assign('catalog_list', $list['catalog']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    make_json_result($smarty->fetch('catalog_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
        
}elseif ($_REQUEST['act'] == 'edit_catalog_name'){
	
    check_authz_json('edit_catalog');

    $id  = intval($_POST['id']);    
    $val = json_str_iconv(trim($_POST['val']));

    $catalog_name = $exc->get_name($id);

    if (!$exc->is_only('catalog_name', $val, $id))
    {
	        make_json_error(sprintf('期刊名 %s 已经存在', $catalog_name));
    }
    else
    {
        $exc->edit("catalog_name='$val'", $id);

        admin_log($val, 'edit', 'catalog');
        make_json_result(stripslashes($val));
    }   
}elseif ($_REQUEST['act'] == 'catalog_status'){
	
    check_authz_json('edit_catalog');
    
    $id       = intval($_POST['id']);
    $val        = intval($_POST['val']);

    if ($exc->edit("catalog_status = '$val'", $id))
    {
        clear_cache_files();
        make_json_result($val);
    }
}elseif ($_REQUEST['act'] == 'list_pageno'){
	
	admin_priv('view_catalog_pageno');

    $smarty->assign('ur_here',      '期刊页码列表');
    $smarty->assign('action_link',  array('text' => '添加期刊页码', 'href' => 'catalog.php?act=add_pageno&id='.$_GET['id']));
    $smarty->assign('full_page',    1);
    
    $catalog_pageno_list = get_catalog_pageno($_GET['id']);

    $smarty->assign('catalog_list', $catalog_pageno_list['catalog_pageno']);
    $smarty->assign('filter',       $catalog_pageno_list['filter']);
    $smarty->assign('record_count', $catalog_pageno_list['record_count']);
    $smarty->assign('page_count',   $catalog_pageno_list['page_count']);

    assign_query_info();
    
    $smarty->display('catalog_pageno_list.tpl');	
}elseif ($_REQUEST['act'] == 'add_pageno'){
	
    admin_priv('add_catalog_pageno');

    $smarty->assign('form_act', 'insert_pageno');
    
    if (empty($_GET['id']))
    {
        sys_msg('invalid param');
    }
    
    $catalog_id = intval($_GET['id']);
    $sql = "SELECT catalog_id,catalog_name FROM " . $GLOBALS['os']->table('catalog') .
            " WHERE catalog_id = '$catalog_id'";
    $catalog = $GLOBALS['db']->getRow($sql);
    
    if (empty($catalog))
    {
        sys_msg('您要操作的目录期刊不存在');
    }
    
    $smarty->assign('catalog_pageno', $catalog);
    
    $smarty->assign('ur_here', '添加目录期刊页码');
    
    $smarty->assign('action_link', array('href' => 'catalog.php?act=list_pageno&id='.$catalog_id, 'text' => '目录期刊页码管理'));
        
	assign_query_info();
	
	$smarty->display('catalog_pageno_info.tpl');		
}elseif($_REQUEST['act'] == 'insert_pageno'){
    /* 检查权限 */
 	admin_priv('add_catalog_pageno');

    /* 是否添加 */
    $catalog_p_no = array();
    $catalog_p_no = $_POST['catalog_p_no'];
    for($i=0;$i<sizeof($catalog_p_no);$i++){
    	$c_p_no = $db->getOne("SELECT catalog_p_no FROM ".$os->table('catalog_page_no')." WHERE catalog_id=".
    	                            intval($_POST['catalog_id'])." AND catalog_p_no=".intval($catalog_p_no[$i]));
    	if(!empty($c_p_no) && $c_p_no>0){
        	sys_msg('目录期刊页码'.$catalog_p_no[$i].'已经存在', 0, $links);
    	}
    	$db->query("INSERT INTO ".$os->table('catalog_page_no')." VALUES(".intval($_POST['catalog_id']).
    	           ",".intval($catalog_p_no[$i]).",'".$_POST['intro'][$i]."')");
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    $links = array(
        array('href' => 'catalog.php?act=add_pageno&id='.intval($_POST['catalog_id']), 'text' => '继续添加目录期刊页码'),
        array('href' => 'catalog.php?act=list_pageno&id='.intval($_POST['catalog_id']), 'text' => '返回目录期刊页码列表')
    );
    sys_msg('添加目录期刊页码成功', 0, $links);
}elseif($_REQUEST['act']=='remove_pageno'){
    /* 检查权限 */
    admin_priv('drop_catalog_pageno');    

    $sql = "SELECT catalog_name FROM " . $os->table('catalog') . " WHERE catalog_id = " . intval($_GET['id']);
    $catalog_name = $db->getOne($sql);
    /* 通过插件来删除用户 */
    $db->query("DELETE FROM " . $os->table('catalog_page_no') . " WHERE catalog_id = " . intval($_GET['id']) . " AND catalog_p_no=".intval($_GET['p_no']));

    /* 记录管理员操作 */
    admin_log(addslashes($catalog_name.'-第'.intval($_GET['p_no']).'页'), 'remove', 'catalog_pageno');

    /* 提示信息 */
    $link[] = array('text' => '返回上一页', 'href'=>'catalog.php?act=list_pageno&id=' . intval($_GET['id']));
    sys_msg(sprintf('期刊页码 %s 已经删除成功。', $catalog_name.'-第'.intval($_GET['p_no']).'页'), 0, $link);	
}elseif($_REQUEST['act']=='query_pageno'){
    $list = get_catalog_pageno();

    $smarty->assign('catalog_list',    $list['catalog_pageno']);
    $smarty->assign('filter',          $list['filter']);
    $smarty->assign('record_count',    $list['record_count']);
    $smarty->assign('page_count',      $list['page_count']);

    make_json_result($smarty->fetch('catalog_pageno_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));		
}elseif($_REQUEST['act'] == 'add_goods'){
    /* 检查权限 */
    admin_priv(); 
    
	$res = $db->getAll("SELECT catalog_id,catalog_name FROM ".$os->table('catalog')." ORDER BY catalog_id DESC");
	
    $catalog_list = array();
    foreach ($res AS $row)
    {
        $catalog_list[$row['catalog_id']] = $row['catalog_name'];
    }
	$smarty->assign('catalog_list',$catalog_list);
	$smarty->assign('form_act','insert_goods');
	
	assign_query_info();

	$smarty->display('catalog_pageno_addgoods.tpl');	
}elseif($_REQUEST['act'] == 'get_catalog_pageno_list'){
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON;

    $catalog_id = intval($_GET['catalog_id']);

    $arr = $db->getAll("SELECT * FROM ".$os->table('catalog_page_no')." WHERE catalog_id=".$catalog_id." ORDER BY catalog_p_no");
    $opt = array();

    foreach ($arr AS $key => $val)
    {
        $opt[] = array('value' => $val['catalog_p_no'],
                       'text'  => '第'.$val['catalog_p_no'].'页',
                       'data'  => '');
    }

    make_json_result($opt);	
}elseif($_REQUEST['act'] == 'get_goods_list'){
    include_once(ROOT_PATH . 'includes/cls_json.php');
    $json = new JSON;

    $filters = $json->decode($_GET['JSON']);

    $arr = get_goods_list($filters);
    $opt = array();

    foreach ($arr AS $key => $val)
    {
        $opt[] = array('value' => $val['goods_id'],
                       'text'  => $val['goods_name'],
                       'data'  => '');
    }

    make_json_result($opt);
}elseif($_REQUEST['act'] == 'add_catalog_pageno_goods'){
	
    include_once(ROOT_PATH . 'includes/cls_json.php');
    
    $json = new JSON;
    $fittings   = $json->decode($_GET['add_ids']);	
    $arr = $db->getAll("SELECT goods_id,goods_name FROM ".$os->table('goods')." WHERE ".db_create_in($fittings,'goods_id'));
    
    $opt = array();

    foreach ($arr AS $val)
    {
        $opt[] = array('value'      => $val['goods_id'],
                       'text'       => $val['goods_name'],
                       'data'       => '');
    }

    clear_cache_files();
    make_json_result($opt);   	
}elseif($_REQUEST['act']=='insert_goods'){
	if(empty($_POST['goods_ids'])){
        sys_msg('invalid param');		
	}
	$goods_ids = explode(',',$_POST['goods_ids']);
	foreach($goods_ids as $goods_id){
		$c_id = $db->getOne("SELECT catalog_id FROM ".$os->table('catalog_p_no_goods')." WHERE catalog_id=".
		           intval($_POST['catalog_id'])." AND catalog_p_no=".intval($_POST['catalog_p_no']).
		           " AND goods_id=".intval($goods_id));
		if(!empty($c_id) || $c_id>0){
			$goods_name = $db->getOne("SELECT goods_name FROM ".$os->table('goods')." WHERE goods_id=".intval($goods_id));
   			sys_msg(sprintf('期刊%s页码商品已经添加',$goods_name));			
		}
		$db->query("INSERT INTO ".$os->table('catalog_p_no_goods')." VALUES(".intval($_POST['catalog_id']).",".intval($_POST['catalog_p_no']).",".intval($goods_id).")");
	}
    /* 显示提示信息，返回商品列表 */
    $link[] = array('href' => 'catalog.php?act=add_goods', 'text' => '继续期刊页码商品添加');
    sys_msg('期刊页码商品添加成功', 0, $link);
}elseif($_REQUEST['act'] == 'send'){
	
	admin_priv();
	
	$smarty->assign('form_act', 'set_send_user');

    assign_query_info();
    
    $smarty->display('catalog_send.tpl');	
}elseif($_REQUEST['act'] == 'get_catalog_list'){

	check_authz_json('send_catalog');
	
    include_once(ROOT_PATH . 'includes/cls_json.php');
    
    $json = new JSON;
    $filters = $json->decode($_GET['JSON']);
   
    /* 取得数据 */
    $sql = "SELECT catalog_name,catalog_sn,catalog_id FROM " . $GLOBALS['os']->table('catalog') .
           " WHERE catalog_name LIKE '%".json_str_iconv($filters->keyword)."%' OR catalog_sn LIKE ".
           " '%".json_str_iconv($filters->keyword)."%' ORDER BY catalog_id DESC LIMIT 50";
    $arr = $db->getAll($sql);
    
    $opt = array();

    foreach ($arr AS $key => $val)
    {
        $opt[] = array('value' => $val['catalog_id'],
                       'text'  => $val['catalog_name'].'-'.$val['catalog_sn'],
                       'data'  => '');
    }

    make_json_result($opt);
}elseif($_REQUEST['act'] == 'add_catalog'){
	
	check_authz_json('send_catalog');
		
    include_once(ROOT_PATH . 'includes/cls_json.php');
    
    $json = new JSON;
    $fittings   = $json->decode($_GET['add_ids']);	
    $arr = $db->getAll("SELECT catalog_id,catalog_name FROM ".$os->table('catalog')." WHERE ".db_create_in($fittings,'catalog_id')." ORDER BY catalog_id DESC");
    
    $opt = array();

    foreach ($arr AS $val)
    {
        $opt[] = array('value'      => $val['catalog_id'],
                       'text'       => $val['catalog_name'],
                       'data'       => '');
    }

    clear_cache_files();
    make_json_result($opt);    
}elseif($_REQUEST['act']=='set_send_user'){
	
	admin_priv('send_catalog');

    $smarty->assign('ur_here',      '目录期刊派发');
    $smarty->assign('full_page',    1);
    if($_GET['user_type'] == 0){
    	$list = get_rosterlist();    	
    	$smarty->assign('user_list', $list['roster']);
    }elseif($_GET['user_type'] == 1){
    	$list = get_userlist();
    	$smarty->assign('user_list', $list['user_list']);
    }
    
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);
	$smarty->assign('form_act', 'send_catalog');

    assign_query_info();
    
    $smarty->display('catalog_send_user.tpl');	
}elseif($_REQUEST['act']=='query_user'){    
   if($_REQUEST['user_type'] == 0){
    	$list = get_rosterlist();    	
    	$smarty->assign('user_list', $list['roster']);
    }elseif($_REQUEST['user_type'] == 1){
    	$list = get_userlist();
    	$smarty->assign('user_list', $list['user_list']);
    }

    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);
    
    make_json_result($smarty->fetch('catalog_send_user.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));	
}elseif($_REQUEST['act']=='send_catalog'){   
	admin_priv('send_catalog');
	
	$catalog_ids = array();
	$catalog_ids = explode(',',$_POST['catalog_ids']);
	
	$user_list = array();
	$user_list = $_POST['checkboxes'];
	
	$user_type = intval($_POST['user_type']);
	
	foreach ($catalog_ids as $catalog_id){
		foreach ($user_list as $uu){
			$db->query("INSERT INTO ".$os->table('catalog_send')."(catalog_id,user_type,user_id,send_status,operate_time) ".
			           " VALUES('$catalog_id', '$user_type', '$uu',1,".gmtime().")");			
		}
	}
    $links = array(
        array('href' => 'catalog.php?act=send', 'text' => '继续目录期刊派发'),
        array('href' => 'catalog.php?act=list', 'text' => '返回目录期刊列表')
    );
    sys_msg('目录期刊派发成功', 0, $links);	
}elseif($_REQUEST['act'] == 'list_send'){
	admin_priv('veiw_send_catalog');		
    $smarty->assign('ur_here',      '派发目录明细列表');
    $smarty->assign('full_page',    1);
    
	if($_GET['user_type']==0){
		$sql = "SELECT cs.send_id,cs.send_status,cs.operate_time,c.catalog_name,r.consignee FROM ".$os->table('catalog_send')." cs LEFT JOIN ".
			   $os->table('catalog')." c ON cs.catalog_id = c.catalog_id LEFT JOIN ".$os->table('roster')." r ON cs.user_id=r.roster_id ".
			   " WHERE cs.user_type=0 AND cs.user_id=".intval($_GET['user_id']." ORDER BY cs.send_id DESC");
	}elseif($_GET['user_type']==1){
		
	}
	
    $res = $GLOBALS['db']->query($sql);
    $send_cataloglist = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
    	$status_list   =  array( 0 =>'未发送', 1 => '已发送');
    	$row['send_status'] = in_array(intval($row['send_status']), array(0, 1)) ? $status_list[$row['send_status']] : '未知';    	
    	$row['operate_time']   = local_date($GLOBALS['_CFG']['time_format'], $row['operate_time']);
        $send_cataloglist[] = $row;
    }	
	$smarty->assign('send_cataloglist',$send_cataloglist);
	
	$smarty->assign('guide',$guide);

    assign_query_info();
    
    $smarty->display('catalog_sendlist.tpl');	
}

function get_cataloglist(){
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keywords']    = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keywords'] = json_str_iconv($filter['keywords']);
        }
        $filter['is_going']   = empty($_REQUEST['is_going']) ? 0 : 1;
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'catalog_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $where = "";
        if (!empty($filter['keywords']))
        {
            $where .= " AND catalog_name LIKE '%" . mysql_like_quote($filter['keywords']) . "%'";
        }
        if($filter['is_going'] == 1){
            $now = gmtime();
            $where .= " AND start_time <= '$now' AND end_time >= '$now' ";
        }

        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('catalog') .
                " WHERE 1 $where";
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        /* 分页大小 */
        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT * ".
                "FROM " . $GLOBALS['os']->table('catalog') .
                " WHERE 1 $where ".
                " ORDER BY $filter[sort_by] $filter[sort_order] ".
                " LIMIT ". $filter['start'] .", $filter[page_size]";

        $filter['keywords'] = stripslashes($filter['keywords']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->query($sql);

    $list = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $row['start_time']  = local_date('Y-m-d H:i', $row['start_time']);
        $row['end_time']    = local_date('Y-m-d H:i', $row['end_time']);

        $list[] = $row;
    }

    return array('catalog' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

function catalog_info($catalog_id){
    $sql = "SELECT * FROM " . $GLOBALS['os']->table('catalog') .
            " WHERE catalog_id = '$catalog_id'";
    $row = $GLOBALS['db']->getRow($sql);
    if (!empty($row))
    {
        $row['start_time'] = local_date($GLOBALS['_CFG']['time_format'], $row['start_time']);
        $row['end_time'] = local_date($GLOBALS['_CFG']['time_format'], $row['end_time']);
    }

    return $row;	
}

function get_catalog_pageno(){
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'catalog_p_no' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'ASC' : trim($_REQUEST['sort_order']);
        $filter['id'] = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);

        $where = " AND catalog_id=".intval($filter['id']);
        if (!empty($filter['keyword']))
        {
            $where .= " AND catalog_p_no LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
        }

        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('catalog_page_no') .
                " WHERE 1 $where";
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        /* 分页大小 */
        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT * ".
                "FROM " . $GLOBALS['os']->table('catalog_page_no') .
                " WHERE 1 $where ".
                " ORDER BY $filter[sort_by] $filter[sort_order] ".
                " LIMIT ". $filter['start'] .", $filter[page_size]";

        $filter['keyword'] = stripslashes($filter['keyword']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->query($sql);

    $list = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {   
    	$row_2 = $GLOBALS['db']->getRow("SELECT catalog_name FROM ".$GLOBALS['os']->table('catalog')." WHERE catalog_id=".intval($row['catalog_id']));
    	$row['catalog_name'] = $row_2['catalog_name'];
        $list[] = $row;
    }

    return array('catalog_pageno' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

function get_userlist()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keywords'] = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keywords'] = json_str_iconv($filter['keywords']);
        }

        $filter['sort_by']    = empty($_REQUEST['sort_by'])    ? 'user_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC'     : trim($_REQUEST['sort_order']);
        $filter['user_type'] = empty($_REQUEST['user_type']) ? 0 : intval($_REQUEST['user_type']);
        $filter['catalog_ids'] = empty($_REQUEST['catalog_ids']) ? 0 : $_REQUEST['catalog_ids'];
        

        $ex_where = ' WHERE 1 ';
        if ($filter['keywords'])
        {
            $ex_where .= " AND user_name LIKE '%" . mysql_like_quote($filter['keywords']) ."%'";
        }     

        $filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['os']->table('users') . $ex_where);

        /* 分页大小 */
        $filter = page_and_size($filter);
        $sql = "SELECT user_id, user_name, email, sex, office_phone, home_phone, mobile_phone, reg_time ".
                " FROM " . $GLOBALS['os']->table('users') . $ex_where .
                " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
                " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        $filter['keywords'] = stripslashes($filter['keywords']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $user_list = $GLOBALS['db']->getAll($sql);

    $count = count($user_list);
    for ($i=0; $i<$count; $i++)
    {
 		$sex_list   =  array( 0 =>'保密', 1 => '男', 2 => '女');
    	$user_list[$i]['sex_format'] = in_array(intval($user_list[$i]['sex']), array(0, 1, 2)) ? $sex_list[$user_list[$i]['sex']] : 0;   
        $user_list[$i]['reg_time'] = local_date($GLOBALS['_CFG']['date_format'], $user_list[$i]['reg_time']);
    }

    $arr = array('user_list' => $user_list, 'filter' => $filter,
        'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}
?>